package lumis.portal.structure;

import lumis.portal.PortalException;
import lumis.portal.manager.ManagerFactory;
import lumis.portal.propertybag.Property;
import lumis.portal.propertybag.PropertyBag;

import java.util.List;

/**
 * A generic implementation class for {@link IStructureElementPropertiesAccessor}.
 *
 * @version $Revision: 11804 $ $Date: 2010-07-29 10:06:21 -0300 (Thu, 29 Jul 2010) $
 * @since 5.6.0
 */
public class GenericStructureElementPropertiesAccessor implements IStructureElementPropertiesAccessor
{
	private final StructureElement structureElement;
	
	/**
	 * The constructor for {@link GenericStructureElementPropertiesAccessor}.
	 * 
	 * @param structureElement the element whose properties are accessed.
	 * @since 5.6.0
	 */
	public GenericStructureElementPropertiesAccessor(StructureElement structureElement)
	{
		this.structureElement = structureElement;
	}
	
	public String getPropertyLocalValue(String propertyName)
	{
		String propertyBagId = structureElement.getPropertyBagId();
		String value = null;
		try
		{
			PropertyBag propertyBag = ManagerFactory.getPropertyBagManager().get(propertyBagId);
			Property localProperty = propertyBag.getLocalProperty(propertyName);
			if(localProperty != null)
			{
				List<String> defaultValues = localProperty.getDefaultValues();
				if(defaultValues != null && !defaultValues.isEmpty())
					value = defaultValues.get(0);
			}
		}
		catch (PortalException e) 
		{
			// ignore exceptions
		}
		
		return value;
	}
	
	public String getPropertyValue(String propertyName)
	{
		return structureElement.getPropertyValue(propertyName);
	}

	public void removeProperty(String propertyName)
	{
		structureElement.removeProperty(propertyName);
	}

	public void setPropertyValue(String propertyName, String propertyValue)
	{
		structureElement.setPropertyValue(propertyName, propertyValue);
	}
}
